<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="完成前图片" prop="p1">
                <div>
                    <img :src="ruleForm2.p1" alt="" style="display:inline-block;vertical-align:bottom;width: 50px;height: 50px;border: 1px solid #eee;">
                    <el-button @click="upImage">选择</el-button>
                </div>
            </el-form-item>
            <el-form-item label="完成后图片" prop="p1">
                <div>
                    <img :src="ruleForm2.p2" alt="" style="display:inline-block;vertical-align:bottom;width: 50px;height: 50px;border: 1px solid #eee;">
                    <el-button @click="upImage1">选择</el-button>
                </div>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {quillEditor} from 'vue-quill-editor';
    import pub from '../../utils/pub'
    import upload from '@/components/common/Upload'

    export default {
        components: {
            quillEditor,
            upload,
        },
        data: function () {
            return {
                roleList: [],
                isHasManager: false,
                admin: {},
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                Project: [],
                ruleForm2: {
                    p1: '', // 完成前图片
                    p2: '', // 完成后图片
                },
                rules2: {
                    p1: [
                        {required: true, message: '请选择完成前图片', trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, message: '请选择完成后图片', trigger: 'blur'}
                    ],
                }
            }
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            upImage(){
                let self = this;
                let el = document.createElement('input')
                el.type = 'file';el.accept="image/*"
                el.onchange = (e)=>{
                    self.ruleForm2.p1 = '';
                    self.loadopen()
                    pub.OssUpload(e.target.files[0], (res)=>{
                        self.loadclose()
                        // console.log(res, '哈哈，我拿到了')
                        self.ruleForm2.p1 = res;
                    })
                };
                el.click();
            },
            upImage1(){
                let self = this;
                let el = document.createElement('input')
                el.type = 'file';el.accept="image/*"
                el.onchange = (e)=>{
                    self.ruleForm2.p2 = '';
                    self.loadopen()
                    pub.OssUpload(e.target.files[0], (res)=>{
                        self.loadclose()
                        // console.log(res, '哈哈，我拿到了')
                        self.ruleForm2.p2 = res;
                    })
                };
                el.click();
            },
            // 获取角色信息
            getInfo() {
                // this.clear();
                // if(!this.uid) {
                //     this.ruleForm2.p1 = '';
                //     this.ruleForm2.p2 = '';
                //     return;
                // }
                // this.loadopen();
                // this.config.get('/housinggestate/project/appointment/findById', {id: this.uid}).then(res => {
                //     this.loadclose();
                //     if (res.data.code === '0') {
                //         // console.log(res.data, '获取角色信息');
                //         this.appointment = res.data.checkProjectAppointment;
                //         this.ruleForm2.id = this.appointment.id;
                //         this.ruleForm2.p1 = this.appointment.beforImg;
                //         this.ruleForm2.p2 = this.appointment.afterImg;
                //     } else {
                //         this.$message.error(this.config.handleError(res.data));
                //     }
                // });
            },
            // 清空表单
            clear(){
                this.ruleForm2.p1 = '';
                this.ruleForm2.p2 = '';
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'id': this.uid,
                            'beforImg': this.ruleForm2.p1,
                            'afterImg': this.ruleForm2.p2,
                        };
                        // let url = this.uid ? '/housinggestate/project/appointment/update' : '/housinggestate/project/appointment/add';
                        this.loadopen();
                        this.config.post('/housinggestate/project/appointmentrecord/setFinish', obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
